package cn.wolfcode.trip.web.controller;

import cn.wolfcode.trip.base.domain.HotelDictionaryItem;
import cn.wolfcode.trip.base.mapper.RegionMapper;
import cn.wolfcode.trip.base.query.HotelDictionaryItemQueryObject;
import cn.wolfcode.trip.base.service.IHotelDictionaryItemService;
import cn.wolfcode.trip.base.service.IHotelDictionaryService;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/hotelDictionaryItem")
public class HotelDictionaryItemController {
    @Autowired
    private IHotelDictionaryItemService hotelDictionaryItemService;
    @Autowired
    private IHotelDictionaryService hotelDictionaryService;
    @Autowired
    private RegionMapper regionMapper;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") HotelDictionaryItemQueryObject qo){
        //查询所有的目录
        model.addAttribute("dics",hotelDictionaryService.selectAll());

        //查询所有的明细
        if (qo.getParentId()!=null) {
            model.addAttribute("result",hotelDictionaryItemService.query(qo));

        }

        //查询所有的国内城市
        model.addAttribute("cities",regionMapper.selectByParentId());
        return "hotelDictionaryItem/list";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JSONResult delete(Long id){
        JSONResult result = new JSONResult();

        try {
            hotelDictionaryItemService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("操作失败");
        }

        return result;
    }




    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(HotelDictionaryItem hotelDictionaryItem){
        JSONResult result = new JSONResult();
        try {

            hotelDictionaryItemService.saveOrUpdate(hotelDictionaryItem);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("操作失败");
        }
        return result;
    }


}
